using System;
using System.Globalization;
using System.Windows.Forms;
using Microsoft.SqlServer.Dts.Runtime;

namespace ForEachWatchedFileEnumerator
{
	public class ForEachWatchedFileUI : ForEachEnumeratorUI
	{
		#region Members

		private Connections _cons;
		private Variables _vars;
        private ForEachWatchedFile _fed;
        private ComboBox cboWatchedFolderPath;
        private CheckBox chkIncludeSubdirectories;
        private TextBox txtTimeOut;
        private Label lblWatchedFolderPathSource;

		private readonly System.ComponentModel.Container components = null;
        private Label lblWatchedFolderPath;
        private Label lblFileSpec;
        private Label lblFileSpecSource;
        private ComboBox cboFileSpecSource;
        private ComboBox cboFileSpec;
        private Label lblIncludeSubdirectories;
        private Label lblTimeOut;
        private Label lblProcessExistingFiles;
        private CheckBox chkProcessExistingFiles;
	    private ComboBox cboWatchedFolderPathSource;

	    #endregion

		#region Ctor / InitializeComponent / Dispose
        public ForEachWatchedFileUI()
            : base()
		{
			InitializeComponent();
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}
		#region InitializeComponent
		private void InitializeComponent()
		{
            this.chkIncludeSubdirectories = new System.Windows.Forms.CheckBox();
            this.txtTimeOut = new System.Windows.Forms.TextBox();
            this.lblWatchedFolderPathSource = new System.Windows.Forms.Label();
            this.cboWatchedFolderPathSource = new System.Windows.Forms.ComboBox();
            this.cboWatchedFolderPath = new System.Windows.Forms.ComboBox();
            this.lblWatchedFolderPath = new System.Windows.Forms.Label();
            this.lblFileSpec = new System.Windows.Forms.Label();
            this.lblFileSpecSource = new System.Windows.Forms.Label();
            this.cboFileSpecSource = new System.Windows.Forms.ComboBox();
            this.cboFileSpec = new System.Windows.Forms.ComboBox();
            this.lblIncludeSubdirectories = new System.Windows.Forms.Label();
            this.lblTimeOut = new System.Windows.Forms.Label();
            this.lblProcessExistingFiles = new System.Windows.Forms.Label();
            this.chkProcessExistingFiles = new System.Windows.Forms.CheckBox();
            this.SuspendLayout();
            // 
            // chkIncludeSubdirectories
            // 
            this.chkIncludeSubdirectories.Location = new System.Drawing.Point(186, 154);
            this.chkIncludeSubdirectories.Name = "chkIncludeSubdirectories";
            this.chkIncludeSubdirectories.Size = new System.Drawing.Size(104, 24);
            this.chkIncludeSubdirectories.TabIndex = 2;
            // 
            // txtTimeOut
            // 
            this.txtTimeOut.Location = new System.Drawing.Point(186, 184);
            this.txtTimeOut.Name = "txtTimeOut";
            this.txtTimeOut.Size = new System.Drawing.Size(100, 20);
            this.txtTimeOut.TabIndex = 3;
            // 
            // lblWatchedFolderPathSource
            // 
            this.lblWatchedFolderPathSource.AutoSize = true;
            this.lblWatchedFolderPathSource.Location = new System.Drawing.Point(16, 32);
            this.lblWatchedFolderPathSource.Name = "lblWatchedFolderPathSource";
            this.lblWatchedFolderPathSource.Size = new System.Drawing.Size(145, 13);
            this.lblWatchedFolderPathSource.TabIndex = 4;
            this.lblWatchedFolderPathSource.Text = "Watched Folder Path Source";
            // 
            // cboWatchedFolderPathSource
            // 
            this.cboWatchedFolderPathSource.DisplayMember = "0";
            this.cboWatchedFolderPathSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboWatchedFolderPathSource.FormattingEnabled = true;
            this.cboWatchedFolderPathSource.Items.AddRange(new object[] {
            "DirectInput",
            "Variable",
            "ConnectionManager"});
            this.cboWatchedFolderPathSource.Location = new System.Drawing.Point(186, 29);
            this.cboWatchedFolderPathSource.Name = "cboWatchedFolderPathSource";
            this.cboWatchedFolderPathSource.Size = new System.Drawing.Size(181, 21);
            this.cboWatchedFolderPathSource.TabIndex = 0;
            this.cboWatchedFolderPathSource.SelectedIndexChanged += new System.EventHandler(this.cboWatchedFolderPathSource_SelectedIndexChanged);
            // 
            // cboWatchedFolderPath
            // 
            this.cboWatchedFolderPath.Location = new System.Drawing.Point(186, 60);
            this.cboWatchedFolderPath.Name = "cboWatchedFolderPath";
            this.cboWatchedFolderPath.Size = new System.Drawing.Size(181, 21);
            this.cboWatchedFolderPath.TabIndex = 5;
            // 
            // lblWatchedFolderPath
            // 
            this.lblWatchedFolderPath.AutoSize = true;
            this.lblWatchedFolderPath.Location = new System.Drawing.Point(16, 63);
            this.lblWatchedFolderPath.Name = "lblWatchedFolderPath";
            this.lblWatchedFolderPath.Size = new System.Drawing.Size(108, 13);
            this.lblWatchedFolderPath.TabIndex = 6;
            this.lblWatchedFolderPath.Text = "Watched Folder Path";
            // 
            // lblFileSpec
            // 
            this.lblFileSpec.AutoSize = true;
            this.lblFileSpec.Location = new System.Drawing.Point(16, 130);
            this.lblFileSpec.Name = "lblFileSpec";
            this.lblFileSpec.Size = new System.Drawing.Size(51, 13);
            this.lblFileSpec.TabIndex = 10;
            this.lblFileSpec.Text = "File Spec";
            // 
            // lblFileSpecSource
            // 
            this.lblFileSpecSource.AutoSize = true;
            this.lblFileSpecSource.Location = new System.Drawing.Point(16, 99);
            this.lblFileSpecSource.Name = "lblFileSpecSource";
            this.lblFileSpecSource.Size = new System.Drawing.Size(88, 13);
            this.lblFileSpecSource.TabIndex = 8;
            this.lblFileSpecSource.Text = "File Spec Source";
            // 
            // cboFileSpecSource
            // 
            this.cboFileSpecSource.DisplayMember = "0";
            this.cboFileSpecSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboFileSpecSource.FormattingEnabled = true;
            this.cboFileSpecSource.Items.AddRange(new object[] {
            "DirectInput",
            "Variable"});
            this.cboFileSpecSource.Location = new System.Drawing.Point(186, 96);
            this.cboFileSpecSource.Name = "cboFileSpecSource";
            this.cboFileSpecSource.Size = new System.Drawing.Size(181, 21);
            this.cboFileSpecSource.TabIndex = 7;
            this.cboFileSpecSource.SelectedIndexChanged += new System.EventHandler(this.cboFileSpecSource_SelectedIndexChanged);
            // 
            // cboFileSpec
            // 
            this.cboFileSpec.Location = new System.Drawing.Point(186, 127);
            this.cboFileSpec.Name = "cboFileSpec";
            this.cboFileSpec.Size = new System.Drawing.Size(181, 21);
            this.cboFileSpec.TabIndex = 9;
            // 
            // lblIncludeSubdirectories
            // 
            this.lblIncludeSubdirectories.AutoSize = true;
            this.lblIncludeSubdirectories.Location = new System.Drawing.Point(16, 159);
            this.lblIncludeSubdirectories.Name = "lblIncludeSubdirectories";
            this.lblIncludeSubdirectories.Size = new System.Drawing.Size(112, 13);
            this.lblIncludeSubdirectories.TabIndex = 11;
            this.lblIncludeSubdirectories.Text = "Include Subdirectories";
            // 
            // lblTimeOut
            // 
            this.lblTimeOut.AutoSize = true;
            this.lblTimeOut.Location = new System.Drawing.Point(16, 187);
            this.lblTimeOut.Name = "lblTimeOut";
            this.lblTimeOut.Size = new System.Drawing.Size(47, 13);
            this.lblTimeOut.TabIndex = 12;
            this.lblTimeOut.Text = "TimeOut";
            // 
            // lblProcessExistingFiles
            // 
            this.lblProcessExistingFiles.AutoSize = true;
            this.lblProcessExistingFiles.Location = new System.Drawing.Point(16, 217);
            this.lblProcessExistingFiles.Name = "lblProcessExistingFiles";
            this.lblProcessExistingFiles.Size = new System.Drawing.Size(108, 13);
            this.lblProcessExistingFiles.TabIndex = 14;
            this.lblProcessExistingFiles.Text = "Process Existing Files";
            // 
            // chkProcessExistingFiles
            // 
            this.chkProcessExistingFiles.Location = new System.Drawing.Point(186, 212);
            this.chkProcessExistingFiles.Name = "chkProcessExistingFiles";
            this.chkProcessExistingFiles.Size = new System.Drawing.Size(104, 24);
            this.chkProcessExistingFiles.TabIndex = 13;
            // 
            // ForEachWatchedFileUI
            // 
            this.Controls.Add(this.lblProcessExistingFiles);
            this.Controls.Add(this.chkProcessExistingFiles);
            this.Controls.Add(this.lblTimeOut);
            this.Controls.Add(this.lblIncludeSubdirectories);
            this.Controls.Add(this.lblFileSpec);
            this.Controls.Add(this.lblFileSpecSource);
            this.Controls.Add(this.cboFileSpecSource);
            this.Controls.Add(this.cboFileSpec);
            this.Controls.Add(this.lblWatchedFolderPath);
            this.Controls.Add(this.lblWatchedFolderPathSource);
            this.Controls.Add(this.chkIncludeSubdirectories);
            this.Controls.Add(this.txtTimeOut);
            this.Controls.Add(this.cboWatchedFolderPathSource);
            this.Controls.Add(this.cboWatchedFolderPath);
            this.Name = "ForEachWatchedFileUI";
            this.Size = new System.Drawing.Size(409, 272);
            this.ResumeLayout(false);
            this.PerformLayout();

        }
		#endregion

		#endregion

		#region Initialize
		/// <summary>
		/// This method is called when the ForEachLoop user interface is displayed. During this method the windows form controls 
		/// in this class representing the properties of the ForEachDirectory enumerator are initialized 
		/// from the property values of the enumerator. 
		/// enumerator.
		/// </summary>
        /// <param name="FEEHost">The Host object containing the ForEachDirectory enumerator in the InnerObject property.</param>
		/// <param name="connections">The collection of ConnectionManager objects in the package.</param>
		/// <param name="variables">The collection of Variable objects in the package.</param>
		public override void Initialize(ForEachEnumeratorHost FEEHost, IServiceProvider serviceProvider, Connections connections, Variables variables)
		{
            if (connections == null)
            {
                throw new ArgumentNullException("connections");
            }

            if (FEEHost == null)
            {
                throw new ArgumentNullException("FEEHost");
            }

            if (variables == null)
            {
                throw new ArgumentNullException("variables");
            }

            this._cons = connections;
			this._vars = variables;

            this._fed = FEEHost.InnerObject as ForEachWatchedFile;

			if (this._fed != null)
			{
                chkIncludeSubdirectories.Checked = _fed.IncludeSubdirectories;
                chkProcessExistingFiles.Checked = _fed.ProcessExistingFiles;
                txtTimeOut.Text = _fed.TimeOut.ToString(CultureInfo.InvariantCulture);


                if (this._fed.WatchFolderPathSource == WatchedFolderPathSource.DirectInput)
                {
                    this.cboWatchedFolderPathSource.SelectedIndex = this.cboWatchedFolderPathSource.Items.IndexOf("DirectInput");
                    this.cboWatchedFolderPath.Text = this._fed.WatchFolderPath;
                }
                else if (this._fed.WatchFolderPathSource == WatchedFolderPathSource.Variable)
                {
                    this.cboWatchedFolderPathSource.SelectedIndex = this.cboWatchedFolderPathSource.Items.IndexOf("Variable");

                    foreach (Variable v in this._vars)
                        cboWatchedFolderPath.Items.Add(v.Name);

                    this.cboWatchedFolderPath.SelectedIndex = this.cboWatchedFolderPath.Items.IndexOf(this._fed.WatchFolderPath);
                }
                else
                {
                    this.cboWatchedFolderPathSource.SelectedIndex = this.cboWatchedFolderPathSource.Items.IndexOf("ConnectionManager");

                    foreach (ConnectionManager cm in this._cons)
                        cboWatchedFolderPath.Items.Add(cm.Name);

                    this.cboWatchedFolderPath.SelectedIndex = this.cboWatchedFolderPath.Items.IndexOf(this._fed.WatchFolderPath);
                }

                if (this._fed.FileSpecSource == FileSpecSource.DirectInput)
                {
                    this.cboFileSpecSource.SelectedIndex = this.cboFileSpecSource.Items.IndexOf("DirectInput");
                    this.cboFileSpec.Text = this._fed.FileSpec;
                }
                else
                {
                    this.cboFileSpecSource.SelectedIndex = this.cboFileSpecSource.Items.IndexOf("Variable");

                    foreach (Variable v in this._vars)
                        cboFileSpec.Items.Add(v.Name);

                    this.cboFileSpec.SelectedIndex = this.cboFileSpec.Items.IndexOf(this._fed.FileSpec);
                }
            }
		}
		#endregion

		#region SaveSettings
		/// <summary>
		/// This method is called if the user clicks OK on the ForEachLoop container's editor. The values of the
		/// ForEachDirectory enumerator are updated based on the values contained in the controls in the UserControl.
		/// </summary>
		public override void SaveSettings()
		{
            _fed.IncludeSubdirectories = chkIncludeSubdirectories.Checked;
            _fed.ProcessExistingFiles = chkProcessExistingFiles.Checked;
            _fed.TimeOut = Int32.Parse(txtTimeOut.Text);

            if (this.cboWatchedFolderPathSource.SelectedItem.ToString() == "DirectInput")
            {
                this._fed.WatchFolderPathSource = WatchedFolderPathSource.DirectInput;
                this._fed.WatchFolderPath = this.cboWatchedFolderPath.Text;
            }
            else if (this.cboWatchedFolderPathSource.SelectedItem.ToString() == "ConnectionManager")
            {
                this._fed.WatchFolderPathSource = WatchedFolderPathSource.ConnectionManager;
                this._fed.WatchFolderPath = this.cboWatchedFolderPath.Text;
            }
            else
            {
                this._fed.WatchFolderPathSource = WatchedFolderPathSource.Variable;
                this._fed.WatchFolderPath = this.cboWatchedFolderPath.Text;
            }

            if (this.cboFileSpecSource.SelectedItem.ToString() == "DirectInput")
            {
                this._fed.FileSpecSource = FileSpecSource.DirectInput;
                this._fed.FileSpec = this.cboFileSpec.Text;
            }
            else
            {
                this._fed.FileSpecSource = FileSpecSource.Variable;
                this._fed.FileSpec = this.cboFileSpec.Text;
            }
        }
		#endregion


		#region Control Events

        private void cboWatchedFolderPathSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cboWatchedFolderPath.Items.Clear();

            WatchedFolderPathSource rootDir;

            this.cboWatchedFolderPath.DropDownStyle = ComboBoxStyle.DropDownList;

            if (this.cboWatchedFolderPathSource.SelectedItem.ToString() == "DirectInput")
            {
                this.cboWatchedFolderPath.DropDownStyle = ComboBoxStyle.DropDown;
                rootDir = WatchedFolderPathSource.DirectInput;
            }
            else if (this.cboWatchedFolderPathSource.SelectedItem.ToString() == "ConnectionManager")
            {
                rootDir = WatchedFolderPathSource.ConnectionManager;
            }
            else
            {
                rootDir = WatchedFolderPathSource.Variable;
            }

            SelectWatchedFolderPathSource(rootDir);
        }

        private void SelectWatchedFolderPathSource(WatchedFolderPathSource rootDirSource)
        {
            //Start fresh
            cboWatchedFolderPath.Items.Clear();

            if (rootDirSource == WatchedFolderPathSource.DirectInput)
            {
                // Do nothing
            }
            else if (rootDirSource == WatchedFolderPathSource.Variable)
            {
                foreach (Variable v in _vars)
                {
                    cboWatchedFolderPath.Items.Add(v.Name);
                }
            }
            else
            {
                foreach (ConnectionManager cm in _cons)
                {
                    cboWatchedFolderPath.Items.Add(cm.Name);
                }
            }
        }

        private void cboFileSpecSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cboFileSpec.Items.Clear();

            FileSpecSource rootDir;

            this.cboFileSpec.DropDownStyle = ComboBoxStyle.DropDownList;

            if (this.cboFileSpecSource.SelectedItem.ToString() == "DirectInput")
            {
                this.cboFileSpec.DropDownStyle = ComboBoxStyle.DropDown;
                rootDir = FileSpecSource.DirectInput;
            }
            else
            {
                rootDir = FileSpecSource.Variable;
            }

            SelectFileSpecSource(rootDir);
        }

        private void SelectFileSpecSource(FileSpecSource rootDirSource)
        {
            //Start fresh
            cboFileSpec.Items.Clear();

            if (rootDirSource == FileSpecSource.DirectInput)
            {
                // Do nothing
            }
            else if (rootDirSource == FileSpecSource.Variable)
            {
                foreach (Variable v in _vars)
                {
                    cboFileSpec.Items.Add(v.Name);
                }
            }
        }

        #endregion

    }
}
